package com.fluxtah.jordl.examples.twitter;

import com.fluxtah.jordl.lib.TransformerProvider;
import com.fluxtah.jordl.lib.TransformException;
import com.fluxtah.jordl.lib.HttpRequestHelper;
import com.fluxtah.jordl.lib.WebResponse;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONObject;

public class SetStatusRequest {
	
	private static final String PATH="/statuses/%s";
	
	private final String id;

	private final Tweet a;
	
	public SetStatusRequest(String id, Tweet a){
		this.id = id;
		this.a = a;
		
	}
	
	public String createBody(TransformerProvider transformerProvider) throws TransformException {
		JSONObject target = new JSONObject();
		if(this.a != null) {
			JSONObject targetMember = new JSONObject();
			transformerProvider.get(TweetOutputTransformer.class).transform(this.a, targetMember);
		}
		return target.toString();
	}
	
	public String createUrl(){
		return String.format(PATH, id);
	}
	
	protected SetStatusResponse createResponse(String baseUrl, HttpRequestHelper requestHelper, TransformerProvider transformerProvider)
		throws ClientProtocolException, IOException, TransformException {
		String url = baseUrl + createUrl();
		String body = createBody(transformerProvider);
		WebResponse response = requestHelper.postJson(url, body);
		
		return new SetStatusResponse(transformerProvider, response);
	}
}	
